package br.com.winesys.geta.crud;

import java.awt.Dimension;
import java.awt.Window;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import br.com.winesys.geta.persistence.jpa.JPAEntityManagerFactoryBuilder;

public abstract class JPACadastroTabela<Entity> extends CadastroTabela<Entity> {

    public JPACadastroTabela(Window owner, String title, Dimension size) {
        super(owner, title, size);
    }

    @Override
    protected final void insert(Entity object) {
        EntityManager manager = JPAEntityManagerFactoryBuilder.getEntityManagerFactory(this.getPersistenceUnit()).createEntityManager();
        Exception exception = null;
        try {
            manager.getTransaction().begin();
            manager.persist(object);
            manager.getTransaction().commit();
        } catch (Exception ex) {
            exception = ex;
            try {
                manager.getTransaction().rollback();
            } catch (Exception e) {
            }
        } finally {
            try {
                manager.close();
            } catch (Exception e) {
            }
        }
        if (exception != null) {
            throw new RuntimeException(exception);
        }
    }

    @Override
    protected final void update(Entity object) {
        EntityManager manager = JPAEntityManagerFactoryBuilder.getEntityManagerFactory(this.getPersistenceUnit()).createEntityManager();
        Exception exception = null;
        try {
            manager.getTransaction().begin();
            object = manager.merge(object);
            manager.persist(object);
            manager.getTransaction().commit();
        } catch (Exception ex) {
            exception = ex;
            try {
                manager.getTransaction().rollback();
            } catch (Exception e) {
            }
        } finally {
            try {
                manager.close();
            } catch (Exception e) {
            }
        }
        if (exception != null) {
            throw new RuntimeException(exception);
        }
    }

    @Override
    protected final void delete(Entity object) {
        EntityManager manager = JPAEntityManagerFactoryBuilder.getEntityManagerFactory(this.getPersistenceUnit()).createEntityManager();
        Exception exception = null;
        try {
            manager.getTransaction().begin();
            object = manager.merge(object);
            manager.remove(object);
            manager.getTransaction().commit();
        } catch (Exception ex) {
            exception = ex;
            try {
                manager.getTransaction().rollback();
            } catch (Exception e) {
            }
        } finally {
            try {
                manager.close();
            } catch (Exception e) {
            }
        }
        if (exception != null) {
            throw new RuntimeException(exception);
        }
    }

    @Override
    protected final void list(Map<String, Object> params) {
        EntityManager manager = JPAEntityManagerFactoryBuilder.getEntityManagerFactory(this.getPersistenceUnit()).createEntityManager();
        Exception exception = null;
        List<Entity> result = null;
        try {
            Query query = manager.createQuery(this.getListQuery(params));
            if (params != null) {
                for (String str : params.keySet()) {
                    query.setParameter(str, params.get(str));
                }
            }
            result = query.getResultList();
            this.table.setValues(result);
        } catch (Exception ex) {
            ex.printStackTrace();
            exception = ex;
        } finally {
            try {
                manager.close();
            } catch (Exception e) {
            }
        }
        if (exception != null) {
            throw new RuntimeException(exception);
        }
    }

    protected abstract String getPersistenceUnit();

    protected abstract String getListQuery(Map<String, Object> params);
}
